package com.zilla.android.zillacore.libzilla.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.github.snowdream.android.util.Log;
import com.squareup.picasso.Picasso;
import com.zilla.android.zillacore.libzilla.listener.OnGetEditTextValuesListener;
import com.zilla.android.zillacore.libzilla.util.ReflectUtil;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenze on 13-12-10.
 */
public class ZillaAdapter<T> extends BaseAdapter {

    protected List<T> list = new ArrayList<T>();
    protected LayoutInflater inflater;
    protected int resid = 0;
    protected Class<?> viewHolder;
    protected Context context;
    /**
     * CheckBox事件，传入的模型中，字段值为1代表选中状态
     */
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    public ZillaAdapter(Context context, List<T> list) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public ZillaAdapter(Context context, List<T> list, int resid) {
        this.list = list;
        this.resid = resid;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public ZillaAdapter(Context context, List<T> list, int resid, Class c) {
        this.list = list;
        this.resid = resid;
        this.viewHolder = c;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return new AdapterUtil<T>(context).getView(
                list, inflater,
                resid, viewHolder, onCheckedChangeListener,
                position, convertView, parent);
    }

    public CompoundButton.OnCheckedChangeListener getOnCheckedChangeListener() {
        return onCheckedChangeListener;
    }

    /**
     * 设置checkbox监听事件，传入的模型中，字段值为1代表选中状态。Model可以通过view.getTag()获得
     *
     * @param onCheckedChangeListener
     */
    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getResid() {
        return resid;
    }

    public void setResid(int resid) {
        this.resid = resid;
    }

    public Class<?> getViewHolder() {
        return viewHolder;
    }

    public void setViewHolder(Class<?> viewHolder) {
        this.viewHolder = viewHolder;
    }

    OnGetEditTextValuesListener onGetEditTextValuesListener;

    public OnGetEditTextValuesListener getOnGetEditTextValuesListener() {
        return onGetEditTextValuesListener;
    }

    public void setOnGetEditTextValuesListener(OnGetEditTextValuesListener onGetEditTextValuesListener) {
        this.onGetEditTextValuesListener = onGetEditTextValuesListener;
    }
}
